<?php

use Illuminate\Database\Capsule\Manager as DB;

class FaqController extends CommonController
{
    protected $authExcept = ['index', 'detail'];

    public function indexAction()
    {
        $page = $params['page'] ?? 1;
        $per_page = $params['per_page'] ?? 10;
        $offset = ($page - 1) * $per_page;

        $total = DB::table('faq')->count();
        $data = DB::table('faq')
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($per_page)
            ->get();

        $result = [
            'current_page' => $page,
            'last_page' => ceil($total / $per_page),
            'per_page' => $per_page,
            'total' => $total,
            'data' => $data,
        ];

        json(['code' => 1, 'msg' => '常见问题', 'data' => $result]);
    }

    public function detailAction()
    {
        $id = $this->get('id', 0);
        $data = DB::table('faq')->find($id);
        json(['code' => 1, 'msg' => '问题详情', 'data' => $data]);
    }
}
